/**
 * Created by liyuanquan on 2017/2/8.
 */
// 相关路径
var findRecipeUrl = serverPath + "recipe/findRecipe";
var findRecipeDetailUrl = serverPath + "recipe/findRecipeDetailById";
var addRecipeUrl = serverPath + "recipe/addRecipe";
var copyRecipeUrl = serverPath + "recipe/copyRecipe";
var deleteRecipeUrl = serverPath + "recipe/deleteRecipeById";
var updataRecipeUrl = serverPath + 'recipe/upgradeRecipe';
var editRecipeUrl = serverPath + 'recipe/updateRecipe';
var selectedBOMDetailUrl = serverPath + 'Boms/findById';

// 与工艺路径相关url
var craftDetailUrl = serverPath + 'RecipePath/getRecipePathDetail';

// 与版本相关url
var ObjVersionChangeUrl = serverPath + 'versionHistory/queryVersionHistoryById';
var ObjHistoryUrl = serverPath + 'versionHistory/queryVersionHistoryByTypeIdAndObjectId';
var ObjVersionChangeStatusUrl = serverPath + 'versionProcess/queryOptionalState';
var ObjVersionAddVersionHistoryUrl = serverPath + 'versionHistory/addVersionHistory';

// 弹出层相关索引
var addRecipeIndex = 0;
var copyRecipeIndex = 0;
var recipeVersionIndex = 0;
var recipeVersionHistoryIndex = 0;
var recipeVersionChangeIndex = 0;
var updataRecipeVersionIndex = 0;
var editRecipeIndex = 0;

// 新增画面相关索引
var selectProduIndex = 0;
var selectUnitIndex = 0;
var selectBomIndex = 0;
var selectPathIndex = 0;

// 定义TypeId查询历史版本
var objectTypeId = 1;
/************************************* 页面加载 *****************************************/
$(function() {
    init();
    var searchContain = $('#searchText').val();
    FindData(searchContain);

    // 搜索按钮 事件
    $("#searchBtn").click(function() {
        var searchContain = $('#searchText').val();
        FindData(searchContain);
    });
    // 点击左侧信息列表 右侧显示详细信息
    $('#recipeResultTable').on('check.bs.table', function (e, row){
        //拿到行选中的数据
        var recipeId = row["id"];
        var selectedBomId = row["bomId"];
        var pathId = row['pathId'];

        InitBtnStatus();
        //刷新右边的数据显示
        checkRecipeDetail(recipeId);
        LoadCraftDetail(pathId);
        LoadCraftPic(pathId);
        selectedBOMData(selectedBomId);
        LoadMfcInfo(recipeId);              //刷新MFC信息
    });
    // 新增 工艺配方 按钮事件
    $("#addRecipe").click(function() {
        var url = "addRecipe.html";
        addRecipeIndex = ibcpLayer.ShowDiv(url, '新增工艺配方', '450px', '650px',function() {
            // 选择产品代码按钮 事件
            $('#selectProduBtn').on('click',function(){
                SelectProdu();
            });
            // 选择单位按钮 事件
            $('#selectUnitBtn').on('click',function(){
                ShowUomInfo();
            });
            // 选择物料BOM按钮 事件
            $("#selectBomBtn").click(function() {
                selectBom();
            });
            // 选择执行路径按钮 事件
            $("#selectPathBtn").click(function() {
                selectPath();
            });
        });
    });
    // 拷贝 工艺配方按钮 事件
    $("#copyRecipe").click(function() {
        var url = "copyRecipe.html";
        copyRecipeIndex = ibcpLayer.ShowDiv(url, '拷贝工艺配方', '450px', '550px',function() {
            // 选择单位按钮 事件
            $('#copyUnitBtn').on('click',function(){
                SelectUnit();
            });
            // 加载相应数据
            var minBatch = $("#minBatch").val();
            var maxBatch = $("#maxBatch").val();
            var batch = $("#recipeBatch").val();
            var unit = $("#recipeUnit").val();
            var registNo = $("#registNo").val();
            var remark = $("#remark").val();

            $("#copyMinBatch").val(minBatch);
            $("#copyMaxBatch").val(maxBatch);
            $("#copyBatch").val(batch);
            $("#copyUnit").val(unit);
            $("#copyRegistNo").val(registNo);
            $("#copyRemark").val(remark);
        });
    });

    // 查看 工艺配方版本 按钮事件
    $('#versionRecipe').click(function() {
        var url = "../Version/VersionDetailCheck.html";
        recipeVersionIndex = ibcpLayer.ShowDiv(url, '工艺配方当前版本', '400px', '380px',function() {
            $("#VersionDetailTable").bootstrapTable();
            var vsId = $("#versionInfo").attr('vsId');
            loadVersionDetailData(vsId);
        });
    });

    // 查看 版本历史 按钮事件
    $('#versionHistory').click(function() {
        var url = "../Version/ObjectVersionHistory.html";
        recipeVersionHistoryIndex = ibcpLayer.ShowDiv(url, '工艺配方版本历史', '700px', '360px',function() {
            $('#VersionHistoryTable').bootstrapTable();
            var proHistoryId = $("#recipeCode").attr("recipeId");
            checkHistoryVersion(proHistoryId, objectTypeId);
        });
    });

    // 版本变更 按钮事件
    $('#versionChange').click(function() {
        var url = "../Version/ObjectStateChange.html";
        recipeVersionChangeIndex = ibcpLayer.ShowDiv(url, '工艺配方版本变更', '440px', '450px',function() {
            var vsId = $("#versionInfo").attr('vsId');
            // 选择日期从当前日期开始
            var fomrmat = new ibcpDateFormat();
            fomrmat.getServerDate("yyyy-MM-dd",function(dateVal){
                $('#ValidDateStart').attr('min', dateVal);
                $('#ValidDateEnd').attr('min', dateVal);
            });
            GetVsInfo(vsId);
        });
    });

    // 版本升级 按钮事件
    $('#versionUpdata').click(function() {
        var url = "updataRecipe.html";
        updataRecipeVersionIndex = ibcpLayer.ShowDiv(url, '工艺配方版本升级', '350px', '400px',function() {
            $('#updataCode').val($('#recipeCode').val());
            $('#updataName').val($('#recipeName').val());
            $('#updataRemark').val($('#remark').val());
        });
    });

    // 编辑 工艺配方 按钮事件
    $("#editRecipe").click(function() {
        var url = "editRecipe.html";
        editRecipeIndex = ibcpLayer.ShowDiv(url, '编辑工艺配方', '426px', '390px',function() {});
    });
});

//初始化数据
function init() {
    initPageSize();
    InitBtnStatus();
    initMFCInfo();                                  //初始化MFC
};
$(window).resize(function() {
    initPageSize();
    // 加载数据时调整折叠层BOM的高度
    var height_1 = $("#contain2").height();
    $("#content").css("height",height_1 - 60);
    // 加载数据时调整折叠层工艺路径的高度
    var height_2 = $("#contain2").height();
    $("#craftPic").css("height",height_2);
});
// 页面初始化
function initPageSize() {
    $('#contain2').css('height', $('#leftSide').height() -  $('#contain1').height()-10.5);
    var dataHeight = $('#leftSide').height() -  95;
    $('#recipeResultTable').bootstrapTable('refreshOptions',{height:dataHeight});
};
// 初始化 按钮状态
function InitBtnStatus(){
    $('#addRecipe').attr("disabled", false);
    $('#copyRecipe').attr("disabled", true);
    $('#deleteRecipe').attr("disabled", true);
    $('#versionRecipe').attr("disabled", true);
    $('#versionHistory').attr("disabled", true);
    $('#versionChange').attr("disabled", true);
    $('#versionUpdata').attr("disabled", true);
    $('#editRecipe').attr("disabled", true);

    $('#MFC_IN_Add').attr("disabled", true);
    $('#MFC_TM_Add').attr("disabled", true);
    $('#MFC_OT_Add').attr("disabled", true);
    $('#MFC_Edit').attr("disabled", true);
    $('#MFC_Delete').attr("disabled", true);
}
/*********************************** 新增 工艺配方 ***************************************/
// 新增工艺配方 确认 按钮事件
function addConfirm() {
    // 新增相关验证
    var addRecipeCode = $("#addRecipeCode").val();
    var addRecipeName = $("#addRecipeName").val();
    var produtCode = $("#matCode").val();
    var matId = $("#matCode").attr("matId");
    var addBatch = $("#addBatch").val();
    var addUnit = $("#addUnit").val();
    var uomId = $("#addUnit").attr("unitId");
    var addMaxBatch = $("#addMaxBatch").val();
    var addMinBatch = $("#addMinBatch").val();
    var addRegistNo = $("#addRegistNo").val();
    var addBOM = $("#addBOM").val();
    var bomId = $("#addBOM").attr("bomId");
    var addPath = $("#addPath").val();
    var addPathId = $("#addPath").attr("pathId");
    var description = $("#description").val();
    var addReason = $("#addReason").val();

    if(addRecipeCode == ''){
        ibcpLayer.ShowTips("请填入代码！",$('#addRecipeCode'));
        return;
    }
    if(addRecipeName == ''){
        ibcpLayer.ShowTips("请填入名称！",$('#addRecipeName'));
        return;
    }
    if(produtCode == ''){
        ibcpLayer.ShowTips("请选择产品！",$('#matCode'));
        return;
    }
    if(addMinBatch == ''){
        ibcpLayer.ShowTips("请填入最小批量！",$('#addMinBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(addMinBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#addMinBatch"));
        return;
    }
    if(addMaxBatch== ''){
        ibcpLayer.ShowTips("请填入最大批量！",$('#addMaxBatch'));
        return;
    }
    if(parseInt(addMaxBatch) < parseInt(addMinBatch)){
        ibcpLayer.ShowTips("最大批量不能小于最小批量！",$('#addMaxBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(addMaxBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#addMaxBatch"));
        return;
    }
    if(addBatch == ''){
        ibcpLayer.ShowTips("请填入批量！",$('#addBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(addBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#addBatch"));
        return;
    }
    if(parseInt(addBatch) < parseInt(addMinBatch)){
        ibcpLayer.ShowTips("小于最小批量！",$("#addBatch"));
        return;
    };
    if(parseInt(addBatch) > parseInt(addMaxBatch)){
        ibcpLayer.ShowTips("大于最大批量！",$("#addBatch"));
        return;
    };
    if(addUnit == ''){
        ibcpLayer.ShowTips("请选择单位！",$('#addUnit'));
        return;
    }
    if(addRegistNo == ''){
        ibcpLayer.ShowTips("请填入登记号！",$('#addRegistNo'));
        return;
    }
    if(addBOM == ''){
        ibcpLayer.ShowTips("请选择物料BOM！",$('#addBOM'));
        return;
    }
    if(addPath == ''){
        ibcpLayer.ShowTips("请选择执行路径！",$('#addPath'));
        return;
    }
    if(description == ''){
        ibcpLayer.ShowTips("请填入备注！",$('#description'));
        return;
    }
    if(addReason == ''){
        ibcpLayer.ShowTips("请填入新增理由！",$('#addReason'));
        return;
    }
    // 确定之后弹出签名
    CallSign(100,function(sid){
        var datas = {
            "sid":sid,
            "code":addRecipeCode,
            "name":addRecipeName,
            "quantity":addBatch,
            "minQuantity":addMinBatch,
            "maxQuantity":addMaxBatch,
            "registNo":addRegistNo,
            "matId":matId,
            "uomId":uomId,
            "bomId":bomId,
            "pathId":addPathId,
            "description":description,
            "reason":addReason
        };
        ibcpAjax.Insert(addRecipeUrl,datas,true,function(result){
            ibcpLayer.ShowOK('新增成功');
            var newId = result.data;
            // 签名层消失
            SignCancel();
            // 新增层消失
            addCancel();
            // 从新获取数据
            GetNewData(newId);
        });
    })
};
// 新增工艺配方 取消 按钮事件
function addCancel() {
    ibcpLayer.Close(addRecipeIndex);
};
// 选择产品代码按钮 事件
function SelectProdu() {
    ibcpSelectProduct.CallProduct(GetSelectProduct);
};
// 产品代码确定按钮 事件
function GetSelectProduct(selectedData){
    $("#matCode").val(selectedData.name + '(' + selectedData.code + ')');
    $("#matCode").attr("matId",selectedData.id);
    ibcpLayer.Close(selectProduIndex);
}
//// 选择批量单位按钮 事件
//function SelectUnit(){
//    var url = "../Uom/UomChose.html";
//    $("#selectUnit").load(url,function() {
//        $('#uomTypeList').bootstrapTable();
//        $('#uomList').bootstrapTable();
//        // 选择批量单位 取消按钮 事件
//        $("#btnUomCancel").click(function() {
//            ibcpLayer.Close(selectUnitIndex);
//        });
//    });
//    selectUnitIndex = ibcpLayer.ShowDiv($("#selectUnit"),'选择单位', '650px', '400px');
//}

// 选择物料BOM按钮 事件
function selectBom() {
    var url = "selectBOM.html";
    selectBomIndex = ibcpLayer.ShowDiv(url,'选择物料BOM','650px','455px',function() {
        $('#selectBOMResult').bootstrapTable();
        // 选择物料BOM 取消按钮 事件
        $("#selectBOMCancelBtn").click(function() {
            ibcpLayer.Close(selectBomIndex);
        });
    });
};
// 选择物料BOM确认按钮 事件
function GetSelectBOM(selectedData) {
    $("#addBOM").val(selectedData.name + '(' + selectedData.code + ')');
    $("#addBOM").attr("bomId",selectedData.id);
};
// 选择执行路径
function selectPath() {
    var url = "selectCraftPath.html";
    selectPathIndex = ibcpLayer.ShowDiv(url,'选择工艺路径','650px','455px',function() {
        $('#selectCraftResult').bootstrapTable();
        // 选择执行路径 取消按钮 事件
        $("#selectCraftCancelBtn").click(function() {
            ibcpLayer.Close(selectPathIndex);
        });
    });
};
// 选择执行路径确定按钮 事件
function GetSelectPath(selectedData){
    $("#addPath").val(selectedData.name + '(' + selectedData.code + ')');
    $("#addPath").attr("pathId",selectedData.id);
    ibcpLayer.Close(selectPathIndex);
}
/*********************************** 拷贝 工艺配方 ***************************************/
// 拷贝工艺配方 确认 按钮事件
function copyConfirm(){
    var selectedRecipe = $('#recipeResultTable').bootstrapTable('getSelections');
    var copyId = selectedRecipe[0].id;
    // 验证输入情况
    var copyCode = $('#copyCode').val();
    var copyName = $('#copyName').val();
    var copyMinBatch = $("#copyMinBatch").val();
    var copyMaxBatch = $("#copyMaxBatch").val();
    var copyBatch = $("#copyBatch").val();
    var copyUnit = $("#copyUnit").val();
    var copyRegistNo = $("#copyRegistNo").val();
    var copyRemark = $('#copyRemark').val();
    var copyReason = $('#copyReason').val();

    if(copyCode == ''){
        ibcpLayer.ShowTips("请填入代码！",$('#copyCode'));
        return;
    }
    if(copyName == ''){
        ibcpLayer.ShowTips("请填入名称！",$('#copyName'));
        return;
    }
    if(copyMinBatch == ''){
        ibcpLayer.ShowTips("请修改最小批量！",$('#copyMinBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(copyMinBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#copyMinBatch"));
        return;
    }
    if(copyMaxBatch== ''){
        ibcpLayer.ShowTips("请修改最大批量！",$('#copyMaxBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(copyMaxBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#copyMaxBatch"));
        return;
    }
    if(parseInt(copyMaxBatch) < parseInt(copyMinBatch)){
        ibcpLayer.ShowTips("最大批量不能小于最小批量！",$('#copyMaxBatch'));
        return;
    }
    if(copyBatch == ''){
        ibcpLayer.ShowTips("请修改批量！",$('#copyBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(copyBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#copyBatch"));
        return;
    }
    if(parseInt(copyBatch) < parseInt(copyMinBatch)){
        ibcpLayer.ShowTips("小于最小批量！",$("#copyBatch"));
        return;
    };
    if(parseInt(copyBatch) > parseInt(copyMaxBatch)){
        ibcpLayer.ShowTips("大于最大批量！",$("#copyBatch"));
        return;
    };
    if(copyRegistNo == ''){
        ibcpLayer.ShowTips("请输入注册登记号！",$("#copyRegistNo"));
        return;
    }
    if(copyRemark == ''){
        ibcpLayer.ShowTips("请输入备注！",$("#copyRemark"));
        return;
    }
    if(copyReason == ''){
        ibcpLayer.ShowTips("请填入理由！",$('#copyReason'));
        return;
    }
    //签名
    CallSign(100,function(sid){
        var datas = {
            'id':copyId,
            'code':copyCode,
            'name':copyName,
            "quantity":copyBatch,
            "minQuantity":copyMinBatch,
            "maxQuantity":copyMaxBatch,
            "registNo":copyRegistNo,
            'description':copyRemark,
            'reason':copyReason,
            'sid':sid
        };
        ibcpAjax.Insert(copyRecipeUrl,datas,true,function(result){
            ibcpLayer.ShowOK('拷贝成功');
            var newId = result.data;
            //关闭签名
            SignCancel();
            //关闭拷贝
            copyCancel();
            //刷新定位新数据行
            GetNewData(newId);
        });
    })
}
// 拷贝工艺配方 取消 按钮事件
function copyCancel() {
    ibcpLayer.Close(copyRecipeIndex);
};
/*************************************** 删除 工艺配方 ********************************************/
// 工艺配方删除按钮 事件
function deleteRecipe() {
    var selectedRecipe = $('#recipeResultTable').bootstrapTable('getSelections');
    var selectedId = selectedRecipe[0].id;
    var selectedCode = selectedRecipe[0].code;
    ibcpLayer.ShowConfirm('您确认要删除工艺配方 : ' + selectedCode, function(){
        deletedSelected(selectedId);
    });
};

// 删除所选工艺配方
function deletedSelected(selectedDelId){
    CallSign(100,function(sid) {
        var datas = {'sid':sid,'id':selectedDelId};
        ibcpAjax.Delete(deleteRecipeUrl,datas,true,function(){
            ibcpLayer.ShowOK('删除成功！');
            // 签名层消失
            SignCancel();
            // 获取新数据
            FindData();
        });
    });
};
/*********************************** 查看 工艺配方版本 ***************************************/
// 工艺配方详细信息
function loadVersionDetailData(id){
    'use strict';
    var datas = {'id': id};

    ibcpAjax.Select(ObjVersionChangeUrl, datas, true, function(result){
        var data = result.data;
        $('#Object_Detail_Version').val(data.version);
        $('#Object_Detail_Version').attr('readonly', 'readonly');
        $('#Object_Detail_State').val(data.toName);
        $('#Object_Detail_State').attr('readonly', 'readonly');
        $('#Object_Detail_StartTime').val(data.validStart);
        $('#Object_Detail_StartTime').attr('readonly', 'readonly');
        $('#Object_Detail_EndTime').val(data.validEnd);
        $('#Object_Detail_EndTime').attr('readonly', 'readonly');
    });
}
// 关闭工艺配方版本详细信息
function VSDetailClose(){
    layer.close(recipeVersionIndex);
}
/*********************************** 查看 工艺配方版本历史 ***************************************/
// 工艺配方历史版本详细信息
function checkHistoryVersion(objId, typeId){
    'use strict';
    var datas = {'objId': objId, 'typeId': typeId};

    ibcpAjax.Select(ObjHistoryUrl, datas, true, function(result){
        var data = result.data;
        $('#VersionHistoryTable').bootstrapTable('load', data);
    });
}
// 关闭工艺配方版本历史信息
function VSHistoryClose(){
    layer.close(recipeVersionHistoryIndex);
}
/*********************************** 查看 工艺配方版本变更 ***************************************/
// 工艺配方版本变更 确认 按钮事件
var currentVersion = "";

//加载当前版本信息
function GetVsInfo(vId){
    var datas = {'id': vId};
    ibcpAjax.Select(ObjVersionChangeUrl, datas, true, function(result) {
        currentVersion = result.data;
        $('#CurrentVersion').val(currentVersion.version);
        $('#CurrentStatus').val(currentVersion.toName);
        LoadAllowChangeStatusInfo(currentVersion.toStatusId, currentVersion.typeId);
    });
}

//加载可变更状态
function LoadAllowChangeStatusInfo(fromId, typeId){
    var datas = {'fromId': fromId,'typeId': typeId};
    $("#targetStatus").attr('readonly', true);
    $("#ValidDateStart").attr('readonly',true);
    $("#ValidDateEnd").attr('readonly',true);
    $("#ChangeReason").attr('readonly',true);
    $("#btnVCSave").attr('disabled',true);

    ibcpAjax.Select(ObjVersionChangeStatusUrl, datas, true, function(result){
        var data = result.data
        if(data.length > 0){
            $("#targetStatus").attr('readonly', false);
            $("#ValidDateStart").attr('readonly',false);
            $("#ValidDateEnd").attr('readonly',false);
            $("#ChangeReason").attr('readonly',false);
            $("#btnVCSave").attr('disabled',false);

            for(var i=0;i<data.length;i++) {
                var toOptions = "<option value='" + JSON.stringify(data[i]) + "'>" + data[i].toName + "</option>"
                $("#targetStatus").append(toOptions);
                $("#targetStatus").val('');
            }
            //只有一个数据的时候默认选择
            if(data.length == 1){
                $("#targetStatus").get(0).select = 0;
            }
        }
        else {
            ibcpLayer.ShowMsgCB('没有可变更的状态',function(){VSCCancel();});
        }
    });
}

//版本变更确认按钮事件
function VSCSave(){
    var vStart ='';
    var vEnd ='';
    var status = $("#targetStatus").val();
    var start = $("#ValidDateStart").val();
    var end = $("#ValidDateEnd").val();

    if(status == null || status == ""){
        ibcpLayer.ShowTips('请选择状态',$("#targetStatus"));
        return;
    }

    if(start != null && start != ""){
        vStart = start + ' 00:00:00';
    }

    if(end != null && end != ""){
        vEnd = end + ' 23:59:59';
    }

    if(vEnd != '' && vStart != ''){
        if(vEnd <= vStart){
            ibcpLayer.ShowTips('结束日期 早于 开始日期',$("#ValidDateEnd"));
            return;
        }
    }

    var selectToInfo = $.parseJSON(status); //选择的目标状态信息 是字符串 需要转Json

    var signNeedId = selectToInfo.aid;   //签名需要的ID
    var toId = selectToInfo.toId;         //目标状态ID
    var vStart = start + ' 00:00:00';
    var vEnd = end + ' 23:59:59';
    var reason = $("#ChangeReason").val();

    CallSign(signNeedId,function(sid){
        var datas = {'verFlowId': currentVersion.verFlowId,
            'version': currentVersion.version,
            'fromStatusId': currentVersion.toStatusId,
            'toStatusId': toId,
            'validStart': vStart,
            'validEnd': vEnd,
            'sid': sid,
            'reason': reason,
            'objId': currentVersion.objId,
            'typeId': currentVersion.typeId
        };
        //保存
        ibcpAjax.Insert(ObjVersionAddVersionHistoryUrl,datas,false,function(){
            ibcpLayer.ShowOK("操作成功！");
            var recipeId = $("#recipeCode").attr('recipeId');
            // 关闭签名
            SignCancel();
            // 关闭变更
            VSCCancel();
            // 从新获取数据
            GetNewData(recipeId);
        });
    });
};
// 工艺配方版本变更 取消 按钮事件
function VSCCancel(){
    layer.close(recipeVersionChangeIndex);
}
/*********************************** 升级 工艺配方 ***************************************/
// 升级工艺配方确认按钮 事件
function updataConfirm(){
    var selectedRecipe = $('#recipeResultTable').bootstrapTable('getSelections');
    var selectedId = selectedRecipe[0].id;
    //验证
    var updataReason = $('#updataReason').val();
    if(updataReason == ''){
        ibcpLayer.ShowTips("请填入理由！",$('#updataReason'));
        return;
    }
    //签名
    CallSign(100,function(sid){
        var datas = {'id':selectedId,'sid':sid,'reason':updataReason};
        ibcpAjax.Insert(updataRecipeUrl,datas,true,function(result){
            ibcpLayer.ShowOK('升级成功！');
            var newId = result.data;
            //关闭签名
            SignCancel();
            //关闭升级
            updataCancel();
            //刷新定位新数据行
            GetNewData(newId);
        });
    })
}
// 升级工艺配方 取消 按钮事件
function updataCancel() {
    ibcpLayer.Close(updataRecipeVersionIndex);
};
/*********************************** 编辑 工艺配方 ***************************************/
// 编辑工艺配方 确认 按钮事件
function editConfirm() {
    var selectedRecipe = $('#recipeResultTable').bootstrapTable('getSelections');
    var selectedId = selectedRecipe[0].id;
    // 相关验证
    var editMinBatch = $('#editMinBatch').val();
    var editMaxBatch = $("#editMaxBatch").val();
    var editBatch = $("#editBatch").val();
    var editRegistNo = $("#editRegistNo").val();
    var editRemark = $("#editRemark").val();

    if(editMinBatch == ''){
        ibcpLayer.ShowTips("请修改最小批量！",$('#editMinBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(editMinBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#editMinBatch"));
        return;
    }
    if(editMaxBatch== ''){
        ibcpLayer.ShowTips("请修改最大批量！",$('#editMaxBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(editMaxBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#editMaxBatch"));
        return;
    }
    if(parseInt(editMaxBatch) < parseInt(editMinBatch)){
        ibcpLayer.ShowTips("最大批量不能小于最小批量！",$('#editMaxBatch'));
        return;
    }
    if(editBatch == ''){
        ibcpLayer.ShowTips("请修改批量！",$('#editBatch'));
        return;
    }
    if(!ibcpValidate.IsIntegerPositive(editBatch)){
        ibcpLayer.ShowTips("请输入数字！",$("#editBatch"));
        return;
    }
    if(parseInt(editBatch) < parseInt(editMinBatch)){
        ibcpLayer.ShowTips("小于最小批量！",$("#editBatch"));
        return;
    };
    if(parseInt(editBatch) > parseInt(editMaxBatch)){
        ibcpLayer.ShowTips("大于最大批量！",$("#editBatch"));
        return;
    };
    if(editRegistNo == ''){
        ibcpLayer.ShowTips("请输入注册登记号！",$("#editRegistNo"));
        return;
    }
    if(editRemark == ''){
        ibcpLayer.ShowTips("请输入备注！",$("#editRemark"));
        return;
    }
    //签名
    CallSign(100,function(sid) {
        var datas = {
            'sid':sid,
            'description':editRemark,
            'id':selectedId,
            'quantity':editBatch,
            'maxQuantity':editMaxBatch,
            'minQuantity':editMinBatch,
            'registNo':editRegistNo
        };
        ibcpAjax.Update(editRecipeUrl,datas,true,function(newId) {
            ibcpLayer.ShowOK('修改成功！');
            var newId = datas['id'];
            // 签名层消失
            SignCancel();
            // 新增层消失
            editCancel();
            // 从新获取数据
            GetNewData(newId);
        });
    });
};
// 编辑工艺配方 取消 按钮事件
function editCancel() {
    ibcpLayer.Close(editRecipeIndex);
};
/************************************* 相关方法 *****************************************/
// 查找相关字段或代码
function FindData(str){
    var datas = {'str':str};
    ibcpAjax.Select(findRecipeUrl,datas,true,function(result){
        var data = result.data;
        $('#recipeResultTable').bootstrapTable('load', data);
        if(data.length >0) {
            //设置选中行
            $('#recipeResultTable').bootstrapTable('check', 0);
        }
        else {
            clearRecipeData();
        }
    });
}
// 左侧信息列表点击 右侧显示详细信息
function checkRecipeDetail(recipeId){
    var datas = {'id':recipeId};
    ibcpAjax.Select(findRecipeDetailUrl,datas,true,function(result) {
        var data = result.data;
        var status = data["statusName"];
        $("#recipeCode").val(data["code"]);
        $("#recipeCode").attr('recipeId', data["id"]);
        $("#recipeName").val(data["name"]);
        $("#recipeUnit").val(data["symbol"]);
        $("#minBatch").val(data["minQuantity"]);
        $("#recipeBatch").val(data["quantity"]);
        $("#maxBatch").val(data["maxQuantity"]);
        $("#produCode").val(data["matCode"]);
        $("#produName").val(data["matName"]);
        $("#produStandar").val(data["spec"]);
        $("#registNo").val(data["registNo"]);
        $("#versionInfo").val(data["version"]);
        $("#versionInfo").attr('vsId', data["verId"]);
        $("#status").val(status);
        $("#remark").val(data["description"]);

        $('#copyRecipe').attr("disabled", false);
        $('#versionRecipe').attr("disabled", false);
        $('#versionHistory').attr("disabled", false);
        $('#versionChange').attr("disabled", false);
        $('#versionUpdata').attr("disabled", false);

        if(status == "失效"){
            $('#versionChange').attr("disabled", true);
        }

        if(status == "编辑"){
            $('#editRecipe').attr("disabled", false);
            $('#deleteRecipe').attr("disabled", false);
            $('#MFC_IN_Add').attr("disabled", false);
            $('#MFC_TM_Add').attr("disabled", false);
            $('#MFC_OT_Add').attr("disabled", false);
            $('#MFC_Edit').attr("disabled", false);
            $('#MFC_Delete').attr("disabled", false);
        }
        else {
            $('#editRecipe').attr("disabled", true);
            $('#deleteRecipe').attr("disabled", true);
        }
        // 加载数据时调整折叠层BOM的高度
        var height_1 = $("#contain2").height();
        $("#content").css("height",height_1 - 50);
        // 加载数据时调整折叠层工艺路径的高度
        var height_2 = $("#contain2").height();
        $("#craftPic").css("height",height_2);
    });
}
// 折叠层显示BOM详细信息
function selectedBOMData(bomId) {
    var datas = {'id':bomId};
    ibcpAjax.Select(selectedBOMDetailUrl,datas,true,function(result){
        var data = result.data;
        var code = data.code;
        var name = data.name;
        var version = data.version;
        var description = data.description;

        $("#productCode").val(code);
        $("#productName").val(name);
        $("#productVersion").val(version);
        $("#productComment").val(description);

        var material = data.bomDetails;
        $("#materialTable").bootstrapTable('load',material);
    });
};
// 折叠层显示工艺路径详细信息
function LoadCraftDetail(pathId){
    var datas = {'id':pathId};
    ibcpAjax.Select(craftDetailUrl,datas,true,function(result) {
        var data = result.data;
        $("#craftCode").val(data["code"]);
        $("#craftCode").attr('craftId', data["id"]);
        $("#craftName").val(data["name"]);
        $("#craftVersion").val(data["version"]);
        $("#craftVersion").attr('vsId', data["verId"]);
        $("#craftComment").val(data["description"]);
    });
}

function LoadCraftPic(pathId){
    var url = "../Editor/WorkFlowEditor/ProcedureCheck.html?workFlowId=" + pathId;
    $('#craftPic').attr("src",url);
}
// 查询无效字段或代码
function clearRecipeData(){
    $("#contain1 input").val("");
    $("#box1 #craftPath input").val("");
    $("textarea").val("");
    $("#materialTable").bootstrapTable('removeAll');
    $("#box2 #recipeBOM input").val("");
    $('#craftPic').attr("src","");
}
// 刷新数据
function GetNewData(newId){
    var newDatas = {'str':$('#searchText').val()};
    ibcpAjax.Select(findRecipeUrl,newDatas,true,function(results){
        var data = results.data;
        $('#recipeResultTable').bootstrapTable('load', data);
        if(data.length >0) {
            //得到新增行数据
            var nowRow = $('#recipeResultTable').bootstrapTable('getRowByUniqueId',newId);
            //移除新增行数据
            $('#recipeResultTable').bootstrapTable('removeByUniqueId', newId);
            //将新增的数据放置在第一行
            $('#recipeResultTable').bootstrapTable("prepend", nowRow);
            //定位新增行
            $("#recipeResultTable").bootstrapTable('check', 0);
        }
    });
}
//区分失效状态和其他状态
function setUneffectiveRow(row, index) {
    if (row.statusName == '失效') {
        return { css: {"color": "#7f7f7f"}};
    }
    else {
        return {};
    }
}

//**************************************MFC内容*********************************************************


//MFC IN
var MFCDivIndex = 0;                //添加物料流输入页面index
var BomMatSelectDivIndex = 0;
var MatSelectDivIndex = 0;
var UomSelectDivIndex = 0;
var OperateSelectDivIndex = 0;
//MFCOperateFlag : 0,删除，1新增流入项，2新增转换项，3新增流出项，4，修改流入项，5修改转换项，6，修改流出项
var MFCOperateFlag = 0;
//MFC信息加载
function initMFCInfo() {
    'use strict';
    initMFCBtns();
}

//MFC相关按钮事件定义
function initMFCBtns(){
    'use strict';
    //新增物料流输入按钮
    $('#MFC_IN_Add').on('click', function(){
        MFCOperateFlag = 1;
        var url = "MFC_IN.html";
        MFCDivIndex = ibcpLayer.ShowDiv(url, "新增物料输入项", "400px", "400px",function() {
            ListenMFCEvents();
            LoadWeiModeSelect();
        });
    });

    $('#MFC_TM_Add').on('click', function(){
        MFCOperateFlag = 2;
        var url = "MFC_TM.html";
        MFCDivIndex = ibcpLayer.ShowDiv(url, "新增物料转换项", "400px", "450px",function() {
            ListenMFCEvents();
        });
    });

    $('#MFC_OT_Add').on('click', function(){
        MFCOperateFlag = 3;
        var url = "MFC_OT.html";
        MFCDivIndex = ibcpLayer.ShowDiv(url, "新增物料流出项", "400px", "400px",function() {
            ListenMFCEvents();
        });
    });

    $('#MFC_Edit').on('click', function(){
        var mfcInfo = $('#mfcList').bootstrapTable('getSelections')[0];
        var mfcType = mfcInfo.type;

        if(mfcType == 1){
            MFCOperateFlag = 4;
            var url = "MFC_IN.html";
            MFCDivIndex = ibcpLayer.ShowDiv(url, "编辑物料流入项", "400px", "400px",function() {
                ListenMFCEvents();
                EditMfcLoad(mfcInfo);
            });
        }
        if(mfcType == 2){
            MFCOperateFlag = 5;
            var url = "MFC_TM.html";
            MFCDivIndex = ibcpLayer.ShowDiv(url, "编辑物料转换项", "400px", "450px",function() {
                ListenMFCEvents();
                EditMfcLoad(mfcInfo);
            });
        }
        if(mfcType == 3){
            MFCOperateFlag = 6;
            var url = "MFC_OT.html";
            MFCDivIndex = ibcpLayer.ShowDiv(url, "编辑物料流出项", "400px", "400px",function() {
                ListenMFCEvents();
                EditMfcLoad(mfcInfo);
            });
        }
    });

    $('#MFC_Delete').on('click', function(){
        var proSel = $('#mfcList').bootstrapTable('getSelections');
        var delId = proSel[0].id;
        var delMsg = proSel[0].mfcNo;
        var mfcType = proSel[0].type;

        ibcpLayer.ShowConfirm('您确定要删除：' + delMsg, function(){
            mfcDelete(delId, mfcType);
        });
    });
}

//加载MFC信息
function LoadMfcInfo(rpId){
    var datas = {'id':rpId};
    var mfcInfoUrl = serverPath +  'mfc/findMfc';
    ibcpAjax.Select(mfcInfoUrl,datas,true,function(result){
        var data = result.data;
        $('#mfcList').bootstrapTable('load', data);
        if(data.length >0) {
            //设置选中行
            $('#mfcList').bootstrapTable('check', 0);
        }
    });
}

//操作 后 刷新 MFC 信息
function RefreshMfc(){
    var rpId = $("#recipeCode").attr('recipeId');
    LoadMfcInfo(rpId);
}

//取消 MFC 操作
function mfcCancel(){
    ibcpLayer.Close(MFCDivIndex);
}

//订阅事件
function ListenMFCEvents(){
    //---------------------------------新增-------------------------
    if(MFCOperateFlag == 1 || MFCOperateFlag==4){
        $('#btnInOK').on('click', function (){
            mfcInAdd();
        });
        $('#btnInCancel').on('click', function (){
            mfcCancel();
        });
        $('#inMatSelectBtn').on('click', function (){
            ShowBomMatInfo();
        });
        $('#inOpSelectBtn').on('click', function (){
            ShowOperateInfo("inOp");
        });
    }
    if(MFCOperateFlag == 2 || MFCOperateFlag==5){
        $('#btnTmOK').on('click', function (){
            mfcTmAdd();
        });
        $('#btnTmCancel').on('click', function (){
            mfcCancel();
        });
        $('#tmMatSelectBtn').on('click', function (){
            ShowMatInfo();
        });
        $('#tmUpOpSelectBtn').on('click', function (){
            ShowOperateInfo("tmUpOp");
        });
        $('#tmDownOpSelectBtn').on('click', function (){
            ShowOperateInfo("tmDownOp");
        });
        $('#tmContainerUomSelectBtn').on('click', function (){
            ShowUomInfo();
        });
    }
    if(MFCOperateFlag == 3  || MFCOperateFlag==6){
        $('#btnOtOK').on('click', function (){
            mfcOtAdd();
        });
        $('#btnOtCancel').on('click', function (){
            mfcCancel();
        });
        $('#otMatSelectBtn').on('click', function (){
            ShowMatInfo();
        });
        $('#otOpSelectBtn').on('click', function (){
            ShowOperateInfo("otOp");
        });
    }
}

//****************************************流入项处理******************************************/

//----------------------------------------流入项 称量模式
//加载流入项称量模式
function LoadWeiModeSelect(){
    var datas = {};
    var weiModeUrl = serverPath +  'weighing/findAllWeighingMode';
    ibcpAjax.Select(weiModeUrl,datas,true,function(result){
        var data = result.data;
        for(var i=0;i<data.length;i++) {
            var toOptions = "<option value='" + data[i].id + "'>" + data[i].name + "</option>"
            $("#inWeiMode").append(toOptions);
            $("#inWeiMode").val('');
        }
    });
}

//----------------------------------------流入项 Bom物料选择
//为Bom物料项选择界面 添加事件 监听
function ListenBomMatEvents(){
    $('#btnBomMatOK').on('click', function (){
        ChoseBomMatOk();
    });
    $('#btnBomMatCancel').on('click', function (){
        ChoseBomMatCancel();
    });
}

//显示物料选择界面
function ShowBomMatInfo(){
    var rpId = $("#recipeCode").attr('recipeId');
    var bomId = $('#recipeResultTable').bootstrapTable('getSelections')[0].bomId;
    var url = "BOMMatSelect.html";
    BomMatSelectDivIndex = ibcpLayer.ShowDiv(url, "编辑物料流出项", "600px", "400px",function() {
        $('#BomMatList').bootstrapTable();
        LoadBomMatSelectInfo(rpId,bomId);
        ListenBomMatEvents();
    });
}

//加载Bom物料项可选择列表
function LoadBomMatSelectInfo(rpId, bomId){
    var datas = {'recipeId':rpId,'bomId':bomId};
    var bomMatUrl = serverPath +  'Boms/findRemainingBomDetailById';
    ibcpAjax.Select(bomMatUrl,datas,true,function(result){
        var data = result.data;
        $('#BomMatList').bootstrapTable('load', data.bomDetails);
        if(data.bomDetails.length >0) {
            //设置选中行
            $('#BomMatList').bootstrapTable('check', 0);
        }
    });
}

//Bom物料项选择确认
function ChoseBomMatOk(){
    if(MFCOperateFlag == 1 || MFCOperateFlag == 4){
        var bomMatInfo = $('#BomMatList').bootstrapTable('getSelections')[0];
        var bomMatId = bomMatInfo.id;
        $('#inMat').attr('inMatId',bomMatId);
        var txt = bomMatInfo.itemCode + '-' + bomMatInfo.name;
        $('#inMat').val(bomMatInfo.name);
    }
    if(MFCOperateFlag == 2 || MFCOperateFlag == 5){
        $('#tmMat').val(bomMatInfo.name);
    }
    if(MFCOperateFlag == 3 || MFCOperateFlag == 6){
        $('#otMat').val(bomMatInfo.name);
    }
    ChoseBomMatCancel();
}

//Bom物料项选择取消
function ChoseBomMatCancel(){
    ibcpLayer.Close(BomMatSelectDivIndex);
}


//----------------------------------------流入项 操作选择
//显示操作选择界面
function ShowOperateInfo(opElementId){
    var url = "OperateSelect.html";
    OperateSelectDivIndex = ibcpLayer.ShowDiv(url, "编辑物料流出项", "400px", "400px",function() {
        $('#OperateList').bootstrapTable()
        ListenOperateSelectEvents(opElementId);
        LoadOptionsSelect();
    });
}

//为操作选择界面 添加事件 监听
function ListenOperateSelectEvents(opElementId){
    $('#btnOpOK').on('click', function (){
        ChoseOperateOk(opElementId);
    });
    $('#btnOpCancel').on('click', function (){
        ChoseOperateCancel();
    });
}

//加载所有操作信息
function LoadOptionsSelect(){
    var rpInfo = $('#recipeResultTable').bootstrapTable('getSelections')[0];
    var datas = {'id':rpInfo.pathId};
    var optionsUrl = serverPath +  'mfc/findAllOperation';
    ibcpAjax.Select(optionsUrl,datas,true,function(result){
        var data = result.data;
        $('#OperateList').bootstrapTable('load', data);
        if(data.length >0) {
            //设置选中行
            $('#OperateList').bootstrapTable('check', 0);
        }
    });
}

//操作选择确认
function ChoseOperateOk(opElementId){
    //取值
    var opInfo = $('#OperateList').bootstrapTable('getSelections')[0];
    //赋值
    var opSel = opInfo.targetStepName + '('+ opInfo.targetNumber +')'
    $('#'+ opElementId).val(opSel);
    $('#'+ opElementId).attr('opId',opInfo.targetNumber);
    $('#'+ opElementId).attr('opName',opInfo.targetStepName);
    ChoseOperateCancel();
}

//操作选择取消
function ChoseOperateCancel(){
    ibcpLayer.Close(OperateSelectDivIndex);
}

//新增 MFC 流入项
function mfcInAdd(){
    //配方id，bom物料项id，操作id，称量模式id，是否打印标签，输入项编号，称量顺序，签名id
    var inId = $('#inNo').attr('inId');
    var rpId = $("#recipeCode").attr('recipeId');
    var no = $('#inNo').val();
    var matId = $('#inMat').attr('inMatId');
    var opId = $('#inOp').attr('opId');
    var opName = $('#inOp').attr('opName');
    var wMode = $('#inWeiMode').val();
    var wSeq = $('#inWeiSequence').val();
    var inPrint = $('#inPrint').is(':checked');;

    //验证
    if(no == ''){
        ibcpLayer.ShowTips("请填入输入项编号！",$('#inNo'));
        return;
    }
    if(matId == ''){
        ibcpLayer.ShowTips("请选择物料项！",$('#inMat'));
        return;
    }
    if(opId == ''){
        ibcpLayer.ShowTips("请选择操作！",$('#inOp'));
        return;
    }
    //后台验证

    //调后台
    var datas = {'id':inId,
        'inputNo':no,
        'recipeId':rpId,
        'bomDetailId':matId,
        'operationId':opId,
        'opName':opName,
        'weightSeq':wSeq,
        'weightModId':wMode,
        'printFlag':inPrint,
        'sid':0};
    var mfcInUrl = '';
    if(MFCOperateFlag == 1){
        CallSign(100,function(sid){
            datas.sid = sid;
            mfcInUrl = serverPath + 'mfc/addInput';
            ibcpAjax.Insert(mfcInUrl,datas,true,function(){
                SignCancel();
                mfcCancel();
                ibcpLayer.ShowOK('添加成功');
                RefreshMfc();
            });
        })
    }
    if(MFCOperateFlag == 4){
        CallSign(100,function(sid){
            datas.sid = sid;
            mfcInUrl = serverPath + 'mfc/updateInput';
            ibcpAjax.Update(mfcInUrl,datas,true,function(){
                SignCancel();
                mfcCancel();
                ibcpLayer.ShowOK('修改成功');
                RefreshMfc();
            });
        })
    }
}


//****************************************流转项处理******************************************/

//----------------------------------------流转项 物料选择

//显示物料选择界面
function ShowMatInfo(){
    var url = "../Public/SelectMaterial.html";
    MatSelectDivIndex = ibcpLayer.ShowDiv(url, "选择物料", "500px", "560px",function() {
        $("#SelectMatTable").bootstrapTable();
        $('#selectCancelBtn').on('click', function (){
            ibcpLayer.Close(MatSelectDivIndex);
        });
    });
}

//设置选择的物料
function GetSelectMat(selectedData){
    var code = selectedData.code;
    var name = selectedData.name;
    $(".mat").val(name + "(" + code + ")");
    $(".mat").attr("matId",selectedData.id);
    ibcpLayer.Close(MatSelectDivIndex);
}

//选择的单位
function ShowUomInfo(){
    //var url = "../Uom/UomChose.html";
    //$('#UomSelectDiv').load(url, function(){
    //    $('#btnUomCancel').on('click', function (){
    //        ibcpLayer.Close(UomSelectDivIndex);
    //    });
    //    selectUnitIndex = ibcpLayer.ShowDiv($('#UomSelectDiv'), "选择单位", '700px', '420px');
    //});
    ibcpUom.CallUom(function GetSelectUom(unitData){
        $('.unit').val(unitData.symbol);
        $('.unit').attr('unitId',unitData.id);
    })
}

//新增 MFC 流转项
function mfcTmAdd(){
    //配方id，转移项编号，物料代码，前操作id，后操作id，连续生产标志，容器id，容器内计量单位id，最早使用期（分钟），最晚使用期（分钟），签名id
    var tmId = $('#tmNo').attr('tmId');
    var rpId = $("#recipeCode").attr('recipeId');
    var no = $('#tmNo').val();
    var matId = $('#tmMat').attr('matId');
    var opUpId = $('#tmUpOp').attr('opId');
    var opUpName = $('#tmUpOp').attr('opName');
    var opDownId = $('#tmDownOp').attr('opId');
    var opDownName = $('#tmDownOp').attr('opName');
    var tmContinue = $('#tmContinuity').val();
    var tmContainer = $('#tmContainer').val();
    var uomId = $('#tmContainerUom').attr('unitId');
    var tmUseDateFirst = $('#tmUseDateFirst').val();
    var tmUseDateLatest = $('#tmUseDateLatest').val();
    //验证
    if(no == ''){
        ibcpLayer.ShowTips("请填入输入项编号！",$('#tmNo'));
        return;
    }
    if(matId == ''){
        ibcpLayer.ShowTips("请选择物料！",$('#tmMat'));
        return;
    }
    if(opUpId == ''){
        ibcpLayer.ShowTips("请选择前操作！",$('#tmUpOp'));
        return;
    }
    if(opDownId == ''){
        ibcpLayer.ShowTips("请选择后操作！",$('#tmUpOp'));
        return;
    }
    if(tmContinue == 1){
        tmContinue = true;
    }else{
        tmContinue = false;
    }
    //后台验证

    //调后台
    var datas = {'id':tmId,
        'recipeId':rpId,
        'moveNo':no,
        'matId':matId,
        'preOpId':opUpId,
        'preOpName':opUpName,
        'sucOpId':opDownId,
        'sucOpName':opDownName,
        'continueProdFlag':tmContinue,
        'containerTypeId':tmContainer,
        'uomId':uomId,
        'earliestTime':tmUseDateFirst,
        'latestTime':tmUseDateLatest,
        'sid':0};
    var mfcTmUrl = '';
    if(MFCOperateFlag == 2){
        CallSign(100,function(sid){
            mfcTmUrl = serverPath + 'mfc/addMove';
            datas.sid = sid;
            ibcpAjax.Insert(mfcTmUrl,datas,true,function(){
                SignCancel();
                mfcCancel();
                ibcpLayer.ShowOK('添加成功');
                RefreshMfc();
            });
        })
    }
    if(MFCOperateFlag == 5){
        CallSign(100,function(sid){
            mfcTmUrl = serverPath + 'mfc/updateMove';
            datas.sid = sid;
            ibcpAjax.Update(mfcTmUrl,datas,true,function(){
                SignCancel();
                mfcCancel();
                ibcpLayer.ShowOK('修改成功');
                RefreshMfc();
            });
        })
    }
}

//****************************************流出项处理******************************************/

//----------------------------------------流出项 物料选择


//新增 MFC 流出项
function mfcOtAdd(){
    //流出项编号，操作id，物料代码id，容器类型id，签名id，配方id
    var otId = $('#otNo').attr('otId');
    var rpId = $("#recipeCode").attr('recipeId');
    var no = $('#otNo').val();
    var matId = $('#otMat').attr('matId');
    var opId = $('#otOp').attr('opId');
    var opName = $('#otOp').attr('opName');
    var otContainer = $('#otContainer').val();

    //验证
    if(no == ''){
        ibcpLayer.ShowTips("请填入流出项编号！",$('#otNo'));
        return;
    }
    if(matId == ''){
        ibcpLayer.ShowTips("请选择物料！",$('#otMat'));
        return;
    }
    if(opId == ''){
        ibcpLayer.ShowTips("请选择操作！",$('#otOp'));
        return;
    }
    //后台验证

    //调后台
    var datas = {'id':otId,
        'outputNo':no,
        'recipeId':rpId,
        'matId':matId,
        'opId':opId,
        'opName':opName ,
        'containerTypeId':otContainer,
        'sid':0};
    //签名
    var mfcInUrl = '';
    if(MFCOperateFlag == 3){
        mfcInUrl = serverPath +  'mfc/addOutput';
        CallSign(100,function(sid){
            datas.sid = sid;
            ibcpAjax.Insert(mfcInUrl,datas,true,function(){
                SignCancel();
                mfcCancel();
                ibcpLayer.ShowOK('添加成功');
                RefreshMfc();
            });
        })
    }
    if(MFCOperateFlag == 6){
        mfcInUrl = serverPath +  'mfc/updateOutput';
        CallSign(100,function(sid){
            datas.sid = sid;
            ibcpAjax.Update(mfcInUrl,datas,true,function(){
                SignCancel();
                mfcCancel();
                ibcpLayer.ShowOK('修改成功');
                RefreshMfc();
            });
        })
    }
}


//加载编辑项数据
function EditMfcLoad(mfcInfo){
    var mfcId = mfcInfo.id;
    if(MFCOperateFlag == 4){
        EditMfcLoad_IN(mfcId);
    }
    if(MFCOperateFlag == 5){
        EditMfcLoad_TM(mfcId);
    }
    if(MFCOperateFlag == 6){
        EditMfcLoad_OT(mfcId);
    }
}

//加载编辑项数据--流入项
function EditMfcLoad_IN(mfcId){
    var sdatas = {'id':mfcId,'typeId':1};
    var mfcFindUrl = serverPath +  'mfc/findMfcDetail';
    ibcpAjax.Select(mfcFindUrl,sdatas,true,function(results){
        var mfcData = results.data;

        $('#inNo').val(mfcData.inputNo);
        $('#inNo').attr('inId',mfcData.id);
        $('#inNo').attr('readOnly',true);
        $('#inMat').attr('inMatId',mfcData.bomDetailId);
        $('#inMat').val(mfcData.matName + '(' + mfcData.itemCode + ')');
        $('#inOp').attr('opId',mfcData.operationId);
        $('#inOp').attr('opName',mfcData.opName);
        $('#inOp').val(mfcData.opName + '(' + mfcData.operationId + ')');

        var datas = {};
        var weiModeUrl = serverPath +  'weighing/findAllWeighingMode';
        ibcpAjax.Select(weiModeUrl,datas,true,function(result){
            var data = result.data;
            for(var i=0;i<data.length;i++) {
                var toOptions = "<option value='" + data[i].id + "'>" + data[i].name + "</option>"
                $("#inWeiMode").append(toOptions);
                $("#inWeiMode").val('');
            }
            $('#inWeiMode').val(mfcData.weightModId);
        });

        $('#inWeiSequence').val(mfcData.weightSeq);
        if(mfcData.printFlag == true){
            $('#inPrint').attr('checked',true);
        }
    });
}

//加载编辑项数据--转换项
function EditMfcLoad_TM(mfcId){
    var sdatas = {'id':mfcId,'typeId':2};
    var mfcFindUrl = serverPath +  'mfc/findMfcDetail';
    ibcpAjax.Select(mfcFindUrl,sdatas,true,function(results){
        var mfcData = results.data;
        $('#tmNo').val(mfcData.moveNo);
        $('#tmNo').attr('tmId',mfcData.id);
        $('#tmNo').attr('readOnly',true);
        $('#tmMat').attr('matId',mfcData.matId);
        $('#tmMat').val(mfcData.matName + '(' + mfcData.matCode + ')');
        $('#tmUpOp').attr('opId',mfcData.preOpId);
        $('#tmUpOp').attr('opName',mfcData.preOpName);
        $('#tmUpOp').val(mfcData.preOpName + '(' + mfcData.preOpId + ')');
        $('#tmDownOp').attr('opId',mfcData.preOpId);
        $('#tmDownOp').attr('opName',mfcData.sucOpName);
        $('#tmDownOp').val(mfcData.sucOpName + '(' + mfcData.sucOpId + ')');

        if(mfcData.continueProdFlag == true){
            $('#tmContinuity').val(1);
        }else {
            $('#tmContinuity').val(0);
        }

        $('#tmContainer').val(mfcData.containerTypeId);
        $('#tmContainerUom').val(mfcData.uomName);
        $('#tmContainerUom').attr('unitId',mfcData.uomId);
        $('#tmUseDateFirst').val(mfcData.earliestTime);
        $('#tmUseDateLatest').val(mfcData.latestTime);
    });
}

//加载编辑项数据--流出项
function EditMfcLoad_OT(mfcId){
    var sdatas = {'id':mfcId,'typeId':3};
    var mfcFindUrl = serverPath +  'mfc/findMfcDetail';
    ibcpAjax.Select(mfcFindUrl,sdatas,true,function(results){
        var mfcData = results.data;
        $('#otNo').val(mfcData.outputNo);
        $('#otNo').attr('otId',mfcData.id);
        $('#otNo').attr('readOnly',true);
        $('#otMat').attr('matId',mfcData.matId);
        $('#otMat').val(mfcData.matName + '(' + mfcData.matCode + ')');
        $('#otOp').attr('opId',mfcData.opId);
        $('#otOp').attr('opName',mfcData.opName);
        $('#otOp').val(mfcData.opName + '(' + mfcData.opId + ')');
        $('#otContainer').val(mfcData.containerTypeId);
    });
}


//删除 MFC 信息
function mfcDelete(mfcId, mfcType){
    var mfcDeleteUrl = serverPath +  'mfc/deleteMfc';
    //签名
    CallSign(100,function(sid){
        var datas = {'id':mfcId,'typeId':mfcType,'sid':sid};
        ibcpAjax.Delete(mfcDeleteUrl,datas,true,function(){
            SignCancel();
            ibcpLayer.ShowOK('删除成功');
            RefreshMfc();
        });
    })
}